<template>
  <a-modal
    title="编辑"
    :width="600"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
    okText="确认"
    cancelText="取消"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="患者姓名"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['patientName', {initialValue: patient.patientName, rules: [{required: true, message: '请输入姓名', trigger: 'blur'}]}]"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="病例卡号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['patientCard', {initialValue: patient.patientCard, rules: [{required: true, message: '请输入病例卡号', trigger: 'blur'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="状态"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['patientState', {initialValue: patient.patientState, rules: [{required: true, message: '请输入病人状态', trigger: 'blur'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="12">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="床号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input v-decorator="['bedId', {initialValue: patient.bedId, rules: [{required: true, message: '请输入病人床号'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { editPatient } from '@/api/medical/patient'
  import { TreeSelect } from 'ant-design-vue'
  export default {
    name: 'EditPatient',
    components: {
      ATreeSelect: TreeSelect
    },
    data () {
      return {
        treeData: [],
        labelCol: {
          xs: { span: 24 },
          sm: { span: 7 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 21 }
        },
        visible: false,
        confirmLoading: false,
        patient: {},

        form: this.$form.createForm(this)
      }
    },
    methods: {
      edit (record) {
        this.visible = true
        this.patient = record
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            editPatient(Object.assign(values, { 'patientId': this.patient.patientId }))
              .then(res => {
                if (res.data.message !== 'error') {
                  this.visible = false
                  this.confirmLoading = false
                  this.form.resetFields()
                  this.$emit('ok')
                } else {
                  this.$message.error('操作失败！')
                }
              })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
